﻿#ifndef DATAMANAGER_H
#define DATAMANAGER_H

//#include <QObject>
#include "../send.h"

class sendHead :public QObject
{
	Q_OBJECT
public:
	explicit sendHead(QObject *parent = 0) :
		QObject(parent)
	{
		head = 0;
		cmd = 0;
		for(int i = 1;i<25;i++){
			m_data_barrel.insert(i,0);
			m_data_reale.insert(i,0);
		}
		fcc = 0;
		m_hadBarrel = false;
		m_hadPutweigh = false;
        putweighTime = QDateTime();
	}
//    ~sendHead(){
////        delete[] sendheadValue.data;
//    }

	sendHead(const sendHead &other){
		copy(other);
	}

	sendHead &operator=(const sendHead & other){
		copy(other);
		return *this;
	}
	void cleanDataOfBarrel(){
		for(int i = 1;i<25;i++){
			m_data_barrel.insert(i,0);
		}
		m_hadBarrel = false;
	}
	void cleanDataOfPutweigh(){
		for(int i = 1;i<25;i++){
			m_data_reale.insert(i,0);
		}
//		valueOfBoxnum = QStringList("");//not clear.
		m_hadPutweigh = false;
        putweighTime = QDateTime();
	}
	void insertBarrel(QMap<int, QString> map){
		QMap<int,QString>::const_iterator itor;
		for(itor = map.constBegin();itor!=map.constEnd();itor ++)
        {
            QString strreplace = itor.value();
//            strreplace = strreplace.replace(".","");
//            if(strreplace.length()<5)
//                strreplace = strreplace.rightJustified(4-strreplace.length(),'0');
//            quint32 value = (strreplace.toInt());
            quint32 value = (strreplace.toFloat()*100);

qDebug()<<"insertBarrel:"<<itor.value()<<itor.value().toDouble()*100<<value<<itor.key()<<strreplace<<strreplace.toInt()<<
                      itor.value().toFloat()*100<<value*100;

			m_data_barrel.insert(itor.key(),value);
		}
		m_hadBarrel = true;
	}
	void insertReale(QMap<int, QString> map){
		QMap<int,QString>::const_iterator itor;
		for(itor = map.constBegin();itor!=map.constEnd();itor ++)
		{
//            quint32 value = (float)itor.value()*100;
            quint32 value = (itor.value().toFloat()*100);

qDebug()<<"insertReale:"<<itor.value()<<itor.value().toDouble()<<value<<itor.key();

			m_data_reale.insert(itor.key(),value);
		}
		m_hadPutweigh = true;
	}
	void insertSingleBarrel(int num,QString value){
//        quint32 qvalue = (float)value*100;
        quint32 qvalue = value.toFloat()*100;
		m_data_barrel.insert(num,qvalue);
		if(!m_hadBarrel)
			m_hadBarrel = true;
	}
	void insertSingleBarrel(int num,float value){
		insertSingleBarrel(num,QString::number(value));
	}

	void insertSingleReale(int num,QString value){
        m_data_reale.insert(num,value.toDouble()*100.00);
		if(!m_hadPutweigh)
			m_hadPutweigh = true;
	}

	void insertGroupFilename(int grpnum,QString filename){
		m_group_filename.insert(grpnum,filename);
	}

	void insetBoxValue(QStringList strlist){
		valueOfBoxnum = strlist;
		qDebug()<<"strlist of boxnum"<<strlist<<valueOfBoxnum;
	}

    QStringList getBoxValueList(){return valueOfBoxnum;}

    void setPutweighTime(QDateTime dt){
        putweighTime = dt;
    }

    QDateTime getPutweighTime(){return putweighTime;}

//    send_head sendheadValue;
	quint32 head;//头部
	quint32  cmd;//命令类型
//    quint32 data[24];//各桶称量
	QMap<int,quint32> m_data_barrel;//各桶称量
	QMap<int,quint32> m_data_reale;//各桶称量

	QStringList valueOfBoxnum;

    QDateTime putweighTime;

	QMap<int,QString> m_group_filename;//各桶称量
	quint32 fcc;//校验码
	bool m_hadBarrel;
	bool m_hadPutweigh;

protected:
	virtual void copy(const sendHead &other){
		this->head= other.head;
		this->cmd= other.cmd;
		this->fcc= other.fcc;
		for(int i=1;i<=24;i++){
			this->m_data_barrel.insert(i,other.m_data_barrel.value(i));
			this->m_data_reale.insert(i,other.m_data_reale.value(i));
		}
		this->m_hadBarrel = other.m_hadBarrel;
		this->m_hadPutweigh = other.m_hadPutweigh;
		this->valueOfBoxnum = other.valueOfBoxnum;
        this->putweighTime = other.putweighTime;
	}

};

class DataManager : public QObject
{
	Q_OBJECT
public:
	explicit DataManager(QObject *parent = 0);

signals:

public slots:

};

#endif // DATAMANAGER_H
